﻿using _网易云.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _网易云
{
    public partial class Form1 : Form
    {
        private IMusicProvider musicProvider = new NetEaseProvider();
        private List<PlayList> playLists = new List<PlayList>();
        private PlayListViewModel currentPlayList = new PlayListViewModel();
        public Form1()
        {
            InitializeComponent();
            this.plSongList.Hide();
            this.plPlayList.VerticalScroll.Visible = true;
            listBox1.DoubleClick += ListBox1_DoubleClick;
            //注册事件
            this.MouseWheel += new MouseEventHandler(FormSample_MouseWheel);
        }

        private void FormSample_MouseWheel(object sender, MouseEventArgs e)
        {
            //获取光标位置
            Point mousePoint = new Point(e.X, e.Y);
            //换算成相对本窗体的位置
            mousePoint.Offset(this.Location.X, this.Location.Y);
            //判断是否在panel内
            if (plPlayList.RectangleToScreen(plPlayList.DisplayRectangle).Contains(mousePoint))
            {
                //滚动
                plPlayList.AutoScrollPosition = new Point(0, plPlayList.VerticalScroll.Value - e.Delta);
            }
        }

        private async void ListBox1_DoubleClick(object sender, EventArgs e)
        {
            var song = listBox1.SelectedItem as SongViewModel;
            var url = await musicProvider.GetUrlById(song.Id.Split('_')[1]);
            if (url == null)
            {
                MessageBox.Show("播放失败,请检查歌曲权限");
            }
            else
            {
                axWindowsMediaPlayer1.URL = url;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            playLists = PlayListSearch.Crawler("http://music.163.com/discover");
            for (int i = 0; i < playLists.Count; i++)
            {
                var playList = playLists[i];
                var pic = new PictureBox();
                pic.Image = Image.FromStream(WebRequest.Create(playList.ImageUrl).GetResponse().GetResponseStream());
                pic.Height = pic.Width = 150;
                pic.Top = 50 + i / 4 * 200;
                pic.Left = 50 + i % 4 * 160;
                //pic.BorderStyle = BorderStyle.Fixed3D;
                pic.SizeMode = PictureBoxSizeMode.StretchImage;
                pic.Click += TiaoZhuan;
                this.plPlayList.Controls.Add(pic);
                pic.Tag = playList.Title;
                var lb = new Label();

                lb.AutoSize = false;
                lb.Location = new System.Drawing.Point(50 + i % 4 * 160, 50 + i / 4 * 200 + 160);
                lb.Size = new System.Drawing.Size(160, 50);
                lb.Text = playList.Title;
                lb.Font = new Font("微软雅黑", 12);
                lb.Click += TiaoZhuan;
                lb.Tag = playList.Title;
                this.plPlayList.Controls.Add(lb);
            }
        }

        private async void TiaoZhuan(object sender, EventArgs e)
        {
            plPlayList.Hide();
            plSongList.Show();
            var control = sender as Control;
            var playTitle = Convert.ToString(control.Tag);
            var playList = playLists.FirstOrDefault(p => p.Title == playTitle);
            if (string.IsNullOrEmpty(playList.Id))
            {
                string albumId = playList.Url.Split('=')[1];
                currentPlayList = await musicProvider.GetAlbum(albumId);
            }
            else
            {
                currentPlayList = await musicProvider.GetPlayList(playList.Id);
            }
            if (currentPlayList == null)
            {
                MessageBox.Show("没有获取到数据,请检查是否为电台节目"); return;
            }

            //var songList = SongListSearch.Crawler(playList.Url);
            listBox1.DataSource = currentPlayList.Tracks;
            listBox1.ValueMember = "Title";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            plPlayList.Show();
            plSongList.Hide();
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            var columnCount = (int)((plPlayList.Width - 50) / 160);
            if (columnCount == 0)
                columnCount = 1;
            int picIndex = 0;
            int lbIndex = 0;
            foreach (var control in this.plPlayList.Controls)
            {
                var picType = typeof(PictureBox);
                var lbType = typeof(Label);
                if (control.GetType() == picType)
                {
                    var pic = control as PictureBox;
                    pic.Top = 50 + picIndex / columnCount * 200;
                    pic.Left = 50 + picIndex % columnCount * 160;
                    picIndex++;
                }
                if (control.GetType() == lbType)
                {
                    var lb = control as Label;
                    lb.Location = new System.Drawing.Point(50 + lbIndex % columnCount * 160, 50 + lbIndex / columnCount * 200 + 160);
                    lbIndex++;
                }

            }
        }
    }


}
